package com.dudu.dao;

import java.sql.Connection;
import java.util.List;

import org.apache.log4j.Logger;

import com.dudu.model.PushNoticeInfo;

import core.CoreDao;
import core.db.DBConnectionUtil;
import net.sf.json.JSONObject;
/**
 * 语音短信推送信息
 * @author weidong
 *
 */
public class PushNoticeInfoDao {
	private static final Logger log =  core.util.Logger.getLogger(PushNoticeInfoDao.class.getSimpleName());
	
	
	/**
	 * 新建语音短信信息
	 * @param dao
	 * @param record 
	 * @return 成功返回0，失败返回-1
	 */
	public int insert(CoreDao dao, PushNoticeInfo record) {
		log.info("t_push_notice_info.insert");
		//数据库连接
		Connection con = null;
		int result = -1;
		try {
			//获取数据库连接
			con = DBConnectionUtil.getDBConnection("emate1");
			//执行sql拼写
			StringBuffer sql = new StringBuffer(
					"INSERT INTO t_push_notice_info(voice_id,push_url,session_id,phone,run_thread,send_count,status,add_time,push_before_time,push_end_time,note)");
			sql.append(" VALUES(?,?,?,?,?,?,?,sysDate(),?,?,?)");
			log.info("insert.sql="+sql);
			//执行sql
			List<JSONObject> list = dao.serviceJsonSql(
					con,
					sql.toString(),
					new Object[] { 
					
						record.getVoice_id(),
						record.getPush_url(),
						record.getSession_id(),
						record.getPhone(),
						record.getRun_thread(),
						record.getSend_count(),
						record.getStatus(),
						record.getPush_before_time(),
						record.getPush_end_time(),
						record.getNote()
						
						});

			JSONObject obj = list.get(0);
			result = obj.optInt("result");

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	
	
	/**
	 * 删除语音短信信息
	 * @param dao
	 * @param id 
	 * @return 成功返回0，失败返回-1
	 */
	public int delete(CoreDao dao, String id) {
		log.info("t_push_notice_info delete");
		//数据库连接
		Connection con = null;
		int result = -1;
		try {
			//获取数据库连接
			con = DBConnectionUtil.getDBConnection("emate1");
			//拼写sql
			StringBuffer sql = new StringBuffer(" delete from t_push_notice_info where id =");
			sql.append("?");
			
			log.info("delete.sql="+sql);
			
			//执行sql
			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(),
					new Object[] { id });

			if (list != null && list.size() > 0) {
				JSONObject obj = list.get(0);
				result = obj.optInt("result");
			}
			
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	
	/**
	 * 编辑语音短信信息
	 * @param dao
	 * @param sendNotice
	 * @return 成功返回0，失败返回-1
	 */
	public int updateStatus(CoreDao dao, PushNoticeInfo sendNotice) {
		log.info("t_push_notice_info.update");
		//数据库连接
		Connection con = null;
		int result = -1;

		try {
			
			//获取数据库连接
			con = DBConnectionUtil.getDBConnection("emate1");
			//拼写SQL
			StringBuffer sql = new StringBuffer(
					" UPDATE t_push_notice_info set status=?");
			sql.append(" where id=? ");
//			log.info("update.sql="+sql);
			//执行sql，更新相应数据
			List<JSONObject> list = dao.serviceJsonSql(
					con,
					sql.toString(),
					new Object[] {
						
						sendNotice.getStatus(),
						sendNotice.getId()
						
			});

			if (list != null && list.size() > 0) {
				JSONObject obj = list.get(0);
				result = obj.optInt("result");
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		
		return result;
	}
	

	public int updateSendTime(CoreDao dao, PushNoticeInfo sendNotice) {
		log.info("PushNoticeInfoDao.update");
		//数据库连接
		Connection con = null;
		int result = -1;

		try {
			
			//获取数据库连接
			con = DBConnectionUtil.getDBConnection("emate1");
			//拼写SQL
			StringBuffer sql = new StringBuffer(
					" UPDATE t_push_notice_info set push_before_time=?,push_end_time=?");
			sql.append(" where id=? ");
			log.info("update.sql="+sql);
			//执行sql，更新相应数据
			List<JSONObject> list = dao.serviceJsonSql(
					con,
					sql.toString(),
					new Object[] {
						
						sendNotice.getPush_before_time(),
						sendNotice.getPush_end_time(),
						sendNotice.getId()
						
			});

			if (list != null && list.size() > 0) {
				JSONObject obj = list.get(0);
				result = obj.optInt("result");
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		
		return result;
	}
	
	
	public int updateSendCount(CoreDao dao, PushNoticeInfo sendNotice) {
		log.info("PushNoticeInfoDao.updateSendCount");
		//数据库连接
		Connection con = null;
		int result = -1;

		try {
			
			//获取数据库连接
			con = DBConnectionUtil.getDBConnection("emate1");
			//拼写SQL
			StringBuffer sql = new StringBuffer(" UPDATE t_push_notice_info set send_count=?");
			
			sql.append(" where id=? ");
			log.info("update.sql="+sql);
			//执行sql，更新相应数据
			
			List<JSONObject> list = dao.serviceJsonSql(
					con,
					sql.toString(),
					new Object[] {
						
						sendNotice.getSend_count(),
						sendNotice.getId()
						
			});

			if (list != null && list.size() > 0) {
				JSONObject obj = list.get(0);
				result = obj.optInt("result");
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		
		return result;
	}
	

	
	public int updateRunThread(CoreDao dao, PushNoticeInfo sendNotice) {
		log.info("t_send_notice_info.update");
		//数据库连接
		Connection con = null;
		int result = -1;

		try {
			
			//获取数据库连接
			con = DBConnectionUtil.getDBConnection("emate1");
			//拼写SQL
			StringBuffer sql = new StringBuffer(" UPDATE t_push_notice_info set run_thread='"+sendNotice.getRun_thread());
			sql.append("' where id="+sendNotice.getId());
			log.info("updateRunThread.sql="+sql);
			//执行sql，更新相应数据
			List<JSONObject> list = dao.serviceJsonSql(
					con,
					sql.toString(),
					new Object[] {
						
						null
						
			});

			if (list != null && list.size() > 0) {
				JSONObject obj = list.get(0);
				result = obj.optInt("result");
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		
		return result;
	}
	

	public List<PushNoticeInfo> queryAllNumber(CoreDao dao) {
		//log.info("t_push_notice_info.queryAllSendNotice");
		//数据库连接
		Connection con = null;
		//最后需返回的
		List<PushNoticeInfo> result = null;
		
		try {
			//获取数据库连接
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer();
			sql.append("select * from t_push_notice_info where (status =-1 or (status = 1 and send_count<3)) and (run_thread  is NULL or run_thread='')");
			//log.info("queryAllNumber="+sql);
			//查询数据
			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(), new Object[] { null });
			//将json类型转化为NumberPool
			
			if (list != null) {
				List<PushNoticeInfo> items = dao.covertToBean(list, PushNoticeInfo.class);
				result = items;
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
		
	}
	
}
